package com.itheima.controller;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.resources.MessageConstant;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Api(tags = "套餐模块管理")
public class SetmealController {


    @Reference
    private SetmealService setmealService;

    @Autowired
    private  StringRedisTemplate redisTemplate;


    @RequestMapping("setmeal/upload")
    @ApiOperation(value = "套餐图片上传", notes = "图片上传到oss")
    public Result upload(@RequestParam("imgFile") MultipartFile file) {
        try {
            //imgFile是前台发送的文件
            String name = file.getOriginalFilename();  //文件名称
            //生成随机名称 防止文件覆盖
            String uuidname = UploadUtils.generateRandonFileName(name);
            //使用阿里云工具类 上传到云
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(), uuidname);

            //把图片存方法哦redis缓存
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,"https://heimazzy.oss-cn-shanghai.aliyuncs.com/"+uuidname);

            //把上传之后的文件名返回给前台
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, uuidname);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


    //删除模块
    @DeleteMapping("setmeal/delete")
    @ApiOperation(value = "删除套餐方法", notes = "删除套餐列表信息")
    public Result delete(@RequestParam("id") Integer id) {
        try {
        //先查询该套餐是否被应用
        int i = setmealService.selectByUser(id);
        if (i==0){
            //先获取要删除的信息对象id  执行物理删除中间表以及setmeal表对应数据
            setmealService.deleteySetmeal(id);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        }else {
            //说明套餐被使用
            return new Result(false, "套餐被使用");
        }


        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }


    //   开发 套餐模块   crud   编辑模块也在这
    @PostMapping("setmeal/add")
    @ApiOperation(value = "检查套餐添加", notes = "根据表单提交数据，录入到数据库")
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        try {
            setmealService.saveSetmeal(setmealDTO);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    //   开发 套餐模块   crud
    @PostMapping("setmeal/findPage")
    @ApiOperation(value = "分页查询套餐方法", notes = "分页查询的套餐表信息")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = setmealService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }

    }

    //编辑修改
    @GetMapping("setmeal/update")
    @ApiOperation(value = "编辑修改套餐方法", notes = "编辑修改套餐列表信息")
    public Result update(@RequestParam("id")int id) {
        System.out.println(id);
        try {
            List<Integer> checkGroupBySetmealId = setmealService.checkGroupBySetmealId(id);
            System.out.println(checkGroupBySetmealId);
            return  new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupBySetmealId);
        } catch (Exception e) {
            e.printStackTrace();
            return   new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

}
